﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using web.data.entities;
using System.Configuration;
using Webdiyer.WebControls.Mvc;
using System.Web.UI;
using System.Linq.Expressions;
using System.Reflection;
using System.Xml.Schema;
using web.component;

namespace web.bussines
{
    public class client_testimonial_service : base_service
    {
        //get all get all
        public PagedList<client_testimonials> adm_client_testimonial_list(int pageindex, int pagesize, int language_id)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_client_testimonials
                            where dt.language_id == language_id
                            //orderby dt.page_id ascending
                            select dt);
                list = list.OrderBy(p => p.order_display);
                return list.ToPagedList(pageindex, pagesize);
            }
        }

        public List<client_testimonials> client_testimonial_list_by_status(int enable, int language_id)
        {
            using (var data = new baseEntities())
            {
                var list = (from dt in data.tbl_client_testimonials
                            where dt.enable == enable && dt.language_id == language_id
                            select dt);
                list = list.OrderBy(p => p.order_display);
                return list.ToList<client_testimonials>();
            }
        }

        public int adm_client_testimonial_update(client_testimonials it)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_client_testimonials.Where(p => p.client_id == it.client_id).SingleOrDefault();
                if (item != null)
                {
                    item.client_id = it.client_id;
                    item.client_name = it.client_name;
                    item.client_email = it.client_email;
                    item.client_phone = it.client_phone;
                    item.client_comment = it.client_comment;
                    item.date_modified = Udate_time.GetCurrentDateTime();
                    item.enable = it.enable;
                    item.order_display = it.order_display;
                    item.language_id = it.language_id;

                    data.SaveChanges();
                    return item.client_id;
                }
                else
                {
                    data.AddTotbl_client_testimonials(it);
                    data.SaveChanges();
                    return it.client_id;
                }
            }
        }
        //enable - disable  
        public int updateStatus(int client_id, int enable)
        {
            var rt = 0;
            using (var data = new baseEntities())
            {
                var edit_item = data.tbl_client_testimonials.Where(p => p.client_id == client_id).SingleOrDefault();

                if (edit_item != null) {
                    edit_item.enable = enable;
                    rt = adm_client_testimonial_update(edit_item);
                }
            } return rt;
        }

        //get items by id or code
        public client_testimonials getByID(int id)
        {
            using (var data = new baseEntities())
            {
                var item = data.tbl_client_testimonials.Where(p => p.client_id == id).FirstOrDefault();
                return item;
            }
        }

        //delete by id
        public void deletePage(int id)
        {
            using (var data = new baseEntities())
            {
                var em = data.tbl_client_testimonials.Where(p => p.client_id == id).FirstOrDefault();

                if (em != null)
                {
                    data.DeleteObject(em);
                    data.SaveChanges();
                }
            }
        }

    }
}
